package com.zjb.demo.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.logicdelete.impl.PrimaryKeyLogicDeleteProcessor;
import com.zjb.demo.common.domain.dto.BaseEntity;
import com.zjb.demo.common.listener.BaseOnListener;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import javax.sql.DataSource;

/**
 * @author 庄佳彬
 * @since 2023/12/22 10:19
 */
@Configuration
public class MybatisFlexConfiguration {
    @Bean(value = "db", typed = true)
    public DataSource dataSource(@Inject("${db}") DruidDataSource druidDataSource) {
        var globalConfig = FlexGlobalConfig.getDefaultConfig();
        globalConfig.setNormalValueOfLogicDelete("0");
//        globalConfig.setDeletedValueOfLogicDelete("2");
        globalConfig.setLogicDeleteColumn("del_flag");
        globalConfig.registerInsertListener(new BaseOnListener(), BaseEntity.class);
        globalConfig.registerUpdateListener(new BaseOnListener(), BaseEntity.class);
        LogicDeleteManager.setProcessor(new PrimaryKeyLogicDeleteProcessor() {
            @Override
            public Object getLogicNormalValue() {
                return "0";
            }
        });
        return druidDataSource;
    }
}
